const app = getApp();
const http = require("../../config/api.js");
const Time = require("../../utils/Timedown.js");
const user = require("../../utils/login.js");
Page({
  data: {
    maskBol: false,
    userInfo: {},
    openid: {},
    baseUrlImg: app.globalData.baseUrlImg
  },
  onLoad: function (options) {
    this.getuserInfo();
  },
  onShow:function() {
    let time = Math.round(new Date().getTime());
    let log = {
      //日志类型：unite.通用业务；visit.访问数据；click.点击数据；order.订购数据；error.错误日志
      logType: "visit",
      msg: JSON.stringify({
        mac: app.globalData.userId,
        page: "xcxsetting",
        pageName: "梨园行设置",
        time: time,
        appVersionName: "小程序",
        item: 65,
        nodeCode: "001000",
        project: "lyhad",
        userId: app.globalData.memberId,
        deviceId: app.globalData.userId,
        uuid: user.getUuid(),
        pageLevel: "1"
      })
    }
    this.uniteLog(log)
  },
  // 获取用户数据
  getuserInfo() {
    if (wx.getStorageSync('userInfo')) {
      this.setData({
        userInfo: JSON.parse(wx.getStorageSync('userInfo')),
        openid: JSON.parse(wx.getStorageSync('openid')),
        ["userInfo.phone"]: (wx.getStorageSync('phone')),
      })
      let VIP = !wx.getStorageSync('VIP') ? {} : JSON.parse(wx.getStorageSync('VIP'));
      if (VIP.orderStatus && VIP.orderStatus != 0) {
        var time = Time.calcTime(VIP.statusMap.lyhad.validDate) * 1
        if (time < 4) {
          this.setData({
            surplusmunber: Math.floor(time),
            surplus: true
          })
          if (time < 1) {
            this.setData({
              surplusmunber: Math.floor(time * 24),
              mathHours: true
            })
          }
        }
        this.setData({
          VIP: VIP
        })
      }
      console.log(this.data.userInfo);
    }
  },
  getPhoneNumber(e) {
    console.log(e.detail.errMsg)
    console.log(e.detail.iv)
    console.log(e.detail.encryptedData)
    if (e.detail.iv){
      var data = {
        "openId": this.data.openid.openId,
        "encryptedData": e.detail.encryptedData,
        "iv": e.detail.iv,
        "memberId": app.globalData.memberId
      }
      var that = this
      wx.request({
        url: 'http://wechat.daoran.tv/API_UBP_TEST/wx/minipro/phone',
        data: data,
        method: 'post',
        header: {
          'content-type': 'application/json' // 默认值
        },
        success(res) {
          console.log(res)
          that.data.userInfo.phone = res.data.obj.phoneNumber
          wx.setStorageSync('phone', res.data.obj.phoneNumber)
          that.setData({
            userInfo: that.data.userInfo
          })
          wx.setStorageSync('userInfo', JSON.stringify(that.data.userInfo))
        }
      })
    }
  },
  // 日志
  uniteLog(data) {
    http.lhxRequest("api_log", data).then(res => {
      if (res.code === 10000000) {
        console.log("日志")
      }
    })
  }
})